﻿@using Telerik.Web.Mvc
@using Telerik.Web.Mvc.UI
@using Presentation.Web.Admin
@model IEnumerable<Domain.Dto.RoleMenuDto>
@{
    ViewBag.subtitle = "角色菜单管理";
    ViewBag.Title = "角色菜单管理";
    var tragetId = 0;
    if (ViewBag.role != null)
    {
        tragetId = ViewBag.role.Id;
        ViewBag.subtitle = "角色菜单管理" + (string.IsNullOrEmpty(ViewBag.role.Name) ? "" : "--" + ViewBag.role.Name);
        ViewBag.Title = "角色菜单管理" + (string.IsNullOrEmpty(ViewBag.role.Name) ? "" : "--" + ViewBag.role.Name);
    }
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<input  id = "tragetId" name = "tragetId" type = "hidden" value = "@(tragetId)"/>
@{ Html.Telerik().Window()
        .Name("addRoleMenuWindow")
        .Title("添加角色菜单")
        .Content(@<text>
    @Html.Partial("_AddMenu")
    </text>)
        .Width(700)
        .Draggable(true).Modal(true).Visible(false)
        .Render();
}
@{Html.Telerik().Grid(Model)
    .Name("RoleMenuGrid")
    .ToolBar(toolBar => toolBar.Template(
    @<text>
    @{
        if (Html.HasUiPermission(this.ViewContext, "addmenu"))
        {
        <a id="addRoleMenu" href="javascript:;" class="t-button">添加菜单</a>
        }
    }
    </text>))
    .Columns(columns =>
    {
        columns.Template(@<text><input name="selectRow" type="checkbox" value="@item.Id " title="checkedRecords" /></text>)
        .HeaderTemplate(@<text><input type="checkbox" title="选择所有" id="checkAllRecords" /></text>)
        .Width(50).HeaderHtmlAttributes(new { style = "text-align:center" }).HtmlAttributes(new { style = "text-align:center" })
        .ClientTemplate("<input type=\"checkbox\" title=\"check all records\" name = 'selectRow' value = '<#= Id #>'/>");
        
        columns.Bound(o => o.Id).Width(50).Title("系统Id").ReadOnly();
        columns.Bound(o => o.Name).Width(200).Title("菜单名");
        columns.Bound(o => o.Remark).Width(200).Title("菜单名描述");
        columns.Bound(o => o.Url).Width(200).Title("导航地址");
        columns.Bound(o => o.CssName).Width(200).Title("样式名");

        columns.Bound(o => o.ParentId).Width(200).Title("所属父Id");
        columns.Bound(o => o.SortNum).Width(50).Title("排序号");
        columns.Bound(o => o.State).Width(100).Title("状态");

        columns.Bound(o => o.IpAddress).Width(100).Title("添加IP").Hidden();
        columns.Bound(o => o.AddTime).Format("{0:yyyy-MM-dd HH:mm:ss}")
            .Width(100).Title("添加时间").ReadOnly().Hidden();
        columns.Command(comm =>
        {
            if (Html.HasUiPermission(this.ViewContext, "editmenu"))
            {
                comm.Edit().ButtonType(GridButtonType.Text);
            }
            if (Html.HasUiPermission(this.ViewContext, "DeleteMenu"))
            {
                comm.Delete().ButtonType(GridButtonType.Text);
            }
        }).Width(150);
    })
    .DataKeys(key => key.Add("Id"))//添加主键
    .ClientEvents(clientEvents => clientEvents.OnRowDataBound("onRowBind"))
    .DataBinding(dataBinding => dataBinding.Ajax().Select("ajaxmenu", "role", new { roleId = tragetId })
        .Update("editmenu", "role", new { roleId = tragetId })
        .Delete("deletemenu", "role", new { roleId = tragetId }))
    .Pageable(p => p.PageSize(15))
    .Groupable()
    .Render();
}
<script type="text/javascript">
    $(function () {
        $('#RoleId').val($('#tragetId').val());
        $('#addRoleMenu').bind('click', function (e) {
            e.preventDefault();
            $('#addRoleMenuWindow').data('tWindow').center().open();
        });
    });

    function onRowBind(e) {
        var columns = $(e.row.outerHTML).children();
        var colStr = "";
        var count = 0;
        $.map(columns, function (item) {
            if (count == 8) {
                if (e.dataItem.State) {
                    $(item).html('<input type="checkbox" disabled="disabled" class="check-box" checked="checked">');
                }
                else {
                    $(item).html('<input type="checkbox" disabled="disabled" class="check-box">');
                }
            }
            colStr += item.outerHTML;
            count++;
        })
        e.row.innerHTML = colStr;
    }
</script>
